package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseStory;
import top.jnaw.jee.utils.Html2Test;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Story extends BaseStory<Story> {

  public static final Story dao = new Story().dao();

  public static ModelResult add(String openid, String type, String title,
      String content, JSONArray images) {
    ModelResult result = new ModelResult(Consts.Result.kModelSaveError);
    Story story = new Story();
    story.setOpenid(openid);
    story.setType(type);
    story.setTitle(title);
    story.setContent(content);
    boolean r = story.save();
    if (r) {
      if (null != images && images.size() > 0) {
        r = StoryImg.add(story.getId(), images);
      }
      result.setObject(story);
      result.setResult(r ? Result.kSuccess : Result.kNext);
    }
    return result;
  }

  public static ModelResult update(int id, String openid, String type,
      String title,
      String content, JSONArray images) {
    ModelResult result = new ModelResult(Consts.Result.kModelSaveError);
    Story story = Story.dao.findById(id);
    if (null != story) {
      story.setOpenid(openid);
      story.setType(type);
      story.setTitle(title);
      story.setContent(content);
      boolean r = story.update();
      if (r) {
        r = StoryImg.update(story.getId(), images);
        result.setObject(story);
        result.setResult(r ? Result.kSuccess : Result.kNext);
      }
    }
    return result;
  }

  public static boolean delete(int id) {
    boolean r = false;
    r = dao.deleteById(id);
    if (r) {
      Db.update(
          "delete from story_img where sid='" + id + "'");
    }
    return r;
  }

  public static JSONObject list(int page, int size, String openid,
      String type) {
    String sec = Db.getSql("story-list-sec");
    String exp = Db.getSql("story-list-exp");
    if (StrKit.notBlank(openid)) {
      exp += " AND s.openid = '" + openid + "'";
    }
    if (StrKit.notBlank(type)) {
      exp += " AND s.type = '" + type + "'";
    }
    exp += " order by create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    String key = "story-query";
    List<Record> list = Db.find(Db.getSql(key), id);
    if (null != list && list.size() > 0) {
      JSONObject object = Models.compressList(list);
      object.put("image", StoryImg.list(id));
      result.setObject(object);
      result.setResult(Consts.Result.kSuccess);
    }
    return result;
  }

}
